package kr.hkit.summary_java.serviceui;

import java.awt.BorderLayout;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import kr.hkit.summary_java.dao.DaoInterface;
import kr.hkit.summary_java.erp.dto.CodeTitle;

public class PanelListUICode extends JPanel {
	
	private JTable table;
	private DaoInterface<CodeTitle> dao;
	private ArrayList<CodeTitle> lists;
	private String[][] arLists;
	
	
	public PanelListUICode() {
		initialize();
	}
	
	public void setDao(DaoInterface<CodeTitle> dao){
		this.dao=dao;
		loadData();
		tableCellAlginAndWidth();
		
	}

	private void initialize() {
		setLayout(new BorderLayout (0,0));
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane);
		
		table = new JTable();
		table.setAutoCreateRowSorter(true);
		scrollPane.setViewportView(table);
		
	}
	

	private void loadData() {
		try {
			lists = dao.listItems();
			arLists = new String[lists.size()][3];
			for(int i=0; i<lists.size();i++){
				arLists[i]=lists.get(i).toArray();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			String[] colNames={"직책번호", "직책명"};
			TableModel model = new DefaultTableModel(arLists, colNames);
			table.setModel(model);
		}
		
	}
	private void tableCellAlginAndWidth() {
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		
		TableColumnModel cModel = table.getColumnModel();
		
		cModel.getColumn(0).setPreferredWidth(70);
		cModel.getColumn(0).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(1).setPreferredWidth(70);
		cModel.getColumn(1).setCellRenderer(dtcrCenter);
	
	}
	
	

	

}
